package com.bluesky.processor;

import com.alibaba.fastjson.JSONObject;
import com.bluesky.utils.HttpClientUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple PageProcessor.
 * @author code4crafter@gmail.com <br>
 * @since 0.1.0
 */
public class SougouImgProcessor {
    private String url;
    private String nexturl;
    private String batchNo;
    private SougouImgPipeline pipeline;
    private List<JSONObject> dataList;
    private List<String> urlList;
    private String word;

    public SougouImgProcessor(String url, String word) {
        this.url = url;
        this.word = word;
        this.pipeline = new SougouImgPipeline();
        this.dataList = new ArrayList<>();
        this.urlList = new ArrayList<>();
    }

    public SougouImgProcessor(String url, String nexturl, String word, String batchNo ) {
        this.url = url;
        this.nexturl = nexturl ;
        this.word = word;
        this.batchNo = batchNo;
        this.pipeline = new SougouImgPipeline();
        this.dataList = new ArrayList<>();
        this.urlList = new ArrayList<>();
    }

    public void process(int idx, int size) {

        String res ;
        List<JSONObject> items = null ;
        // text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
        /*
        * host: pic.sogou.com
            sec-ch-ua:"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"
            sec-ch-ua-mobile:?0
            sec-ch-ua-platform:"Windows"
            sec-fetch-dest:empty
            sec-fetch-mode:cors
            sec-fetch-site:same-origin
            user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
            x-time4p:1727537539462
* */
        Header[] headers = new Header[]{
                new BasicHeader("Accept", "application/json, text/plain, */*"),
                new BasicHeader("Accept-Encoding", "gzip, deflate, br,zstd"),
                new BasicHeader("Accept-Language", "zh-CN,zh;q=0.9"),
                new BasicHeader("Cache-Control", "max-age=0"),
                new BasicHeader("Connection", "keep-alive"),
                new BasicHeader("Cookie","SUV=00DF573E9034B9F366F558EE84A08423; wuid=1727355118504; FUV=25764bb25954edcc0fa3a459f3db071c; SNUID=0A40CD6AFAFFDAF1A10F0D9AFAD318A1; ABTEST=0|1727537504|v1; search_tip=1727537517012"),
                new BasicHeader("host", "pic.sogou.com"),
                new BasicHeader("sec-ch-ua", "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Google Chrome\";v=\"128\""),
                new BasicHeader("sec-ch-ua-mobile", "?0"),
                new BasicHeader("sec-ch-ua-platform", "Windows"),
                new BasicHeader("sec-fetch-dest", "empty"),
                new BasicHeader("sec-fetch-mode", "cors"),
                new BasicHeader("sec-fetch-site", "same-origin"),
                new BasicHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"),
                new BasicHeader("x-time4p", System.currentTimeMillis()+""),

        };
        System.out.println("idx = " + idx + ", size = " + size);
        res =  HttpClientUtils.get(String.format(this.nexturl, idx, size, this.word), headers);
        JSONObject object = JSONObject.parseObject(res);
        if(!"empty".equals(object.getString("info"))  &&  object.get("data")!=null ){
            items = (List<JSONObject>)((JSONObject)object.get("data")).get("items");
            for(JSONObject item : items){
                this.urlList.add(item.getString("picUrl"));
            }
            this.dataList.addAll(items);
        }

    }

    public void processnew(int idx, int size) {

        String res ;
        List<JSONObject> items = null ;
        if(idx == 0 ){
            res =  HttpClientUtils.get(String.format(this.url, this.word));
            int start =  res.lastIndexOf("\"searchList\"")+13;
            int end = res.lastIndexOf("\"colorList\"")-1;
            String substring = res.substring(start, end);
            if(JSONObject.isValidArray(substring)){
                items =  JSONObject.parseArray(substring);
                for(JSONObject item : items){
                    this.urlList.add(item.getString("locImageLink"));
                }
            }
        }else{
            // text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
            Header[] headers = new Header[]{
                    new BasicHeader("Accept", "application/json, text/plain, */*"),
                    new BasicHeader("Accept-Encoding", "gzip, deflate, br,zstd"),
                    new BasicHeader("Accept-Language", "zh-CN,zh;q=0.9"),
                    new BasicHeader("Cache-Control", "max-age=0"),
                    new BasicHeader("Connection", "keep-alive"),
                    new BasicHeader("Cookie","SUV=00DF573E9034B9F366F558EE84A08423; wuid=1727355118504; FUV=25764bb25954edcc0fa3a459f3db071c; SNUID=0A40CD6AFAFFDAF1A10F0D9AFAD318A1; ABTEST=0|1727537504|v1; search_tip=1727537517012")
            };
            res =  HttpClientUtils.get(String.format(this.nexturl, idx, size, this.word), headers);
            JSONObject object = JSONObject.parseObject(res);
            items = (List<JSONObject>)((JSONObject)object.get("data")).get("items");
        }


        /*Document document = Jsoup.parse(res);

        Element element = document.children().get(0);
        Element element1 = element.children().get(1).children().get(0);
        Element element2 = element1.children().get(2);

        Elements imgs = element.getElementsByTag("img");*/



        /*if(JSONObject.isValidArray(substring)){
             items =  JSONObject.parseArray(substring);
            for(JSONObject item : items){
                this.urlList.add(item.getString("locImageLink"));
            }
        }else{
            JSONObject object = JSONObject.parseObject(substring);
            items = (List<JSONObject>)((JSONObject)object.get("data")).get("items");

        }*/



        this.dataList.addAll(items);
    }

    // 下载
    public void pipelineData(){
        // 多线程
        pipeline.processSync(this.urlList, this.word, this.batchNo);
    }

    public static void main(String[] args) {
//        String url = "https://pic.sogou.com/napi/pc/searchList?mode=1&start=%s&xml_len=%s&query=%s";

        String nexturl =  "https://pic.sogou.com/napi/pc/searchList?mode=2&start=%s&xml_len=%s&query=%s&channel=pc_pic&scene=pic_result";

        String url = "https://pic.sogou.com/pics?query=%s&w=05009900";
        // 美女
        SougouImgProcessor processor = new SougouImgProcessor(url,nexturl,"美女","2024092902-");

        int start = 0, size = 50, limit = 1000;
        // 定义爬取开始索引、每次爬取数量、总共爬取数量

        for(int i=start;i<start+limit;i+=size)
            processor.process(i, size);

        processor.pipelineData();

    }
}